<?php

// $Id$


class Mail_Image extends Tangra_Class {
	private $file;
	private $name;
	private $type = 'application/octet-stream';
	private $cid;

	function __construct($file, $name, $autodetect = true, $cid = NULL, $type = NULL) {
		$this->set_cid($cid);

		if ($autodetect) {
			$mime = $this->detect_mime_type($file);
			if ($mime) {
				$this->set_file($file);
				$this->set_name($name);
				$this->set_type($mime);
			} else {
				throw new Tangra_Module_Exception('Cannot detect image type of file: '.$file);
			}
		} else {
			$this->set_file($file);
			$this->set_name($name);
			$this->set_type($type);
		}
	}


	private function detect_mime_type($file) {
		$ret = false;

		if (file_exists($file)) {
			$tmp = getimagesize($file);
			if ($tmp !== false) {
				$ret = $tmp['mime'];
			}
		} else {
			throw new Tangra_Module_Exception('Cannot find file: '.$file);
		}

		return $ret;
	}


	public function set_cid($cid) {
		$this->cid = $cid;
	}


	public function get_cid() {
		return $this->cid;
	}


	public function set_file($file) {
		if (file_exists($file)) {
			$this->file = $file;
		} else {
			throw new Tangra_Module_Exception('Cannot find file: '.$file);
		}
	}


	public function get_file() {
		return $this->file;
	}


	public function set_name($name) {
		$this->name = $name;
	}


	public function get_name() {
		return $this->name;
	}


	public function set_type($type) {
		$this->type = $type;
	}


	public function get_type() {
		return $this->type;
	}
}

